package com.holly.unit.dict.modular.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.holly.unit.core.enums.YesOrNotEnum;
import com.holly.unit.core.pojo.request.BaseRequest;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.dict.api.constants.DictConstants;
import com.holly.unit.dict.modular.entity.SysDict;
import com.holly.unit.dict.modular.entity.SysDictType;
import com.holly.unit.dict.modular.pojo.DictSelect;
import com.holly.unit.dict.modular.pojo.TreeDictInfo;
import com.holly.unit.dict.modular.pojo.request.DictRequest;
import com.holly.unit.dict.modular.service.DictService;
import com.holly.unit.dict.modular.service.DictTypeService;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典详情管理，具体管理某个字典类型下的条目
 *
 * @author holly
 * @date 2020/10/29 14:45
 */
@RestController
@ApiResource(name = "字典详情管理")
@Api(tags = "字典详情管理")
public class DictController {

    @Resource
    private DictService dictService;

    @Resource
    private DictTypeService dictTypeService;

    /**
     * 添加字典条目
     *
     * @author holly
     * @date 2020/10/29 16:35
     */
    @ApiOperation(value = "添加字典", notes = "添加字典")
    @PostResource(name = "添加字典", path = "/dict/add", requiredPermission = false)
    public ResponseData add(@RequestBody @Validated(BaseRequest.add.class) DictRequest dictRequest) {
        this.dictService.add(dictRequest);
        return new SuccessResponseData();
    }

    /**
     * 删除字典条目
     *
     * @author holly
     * @date 2020/10/29 16:35
     */
    @ApiOperation(value = "删除字典", notes = "删除字典")
    @PostResource(name = "删除字典", path = "/dict/delete", requiredPermission = false)
    public ResponseData delete(@RequestBody @Validated(DictRequest.delete.class) DictRequest dictRequest) {
        this.dictService.del(dictRequest);
        return new SuccessResponseData();
    }

    /**
     * 修改字典条目
     *
     * @author holly
     * @date 2020/10/29 16:35
     */
    @ApiOperation(value = "修改字典", notes = "修改字典")
    @PostResource(name = "修改字典", path = "/dict/edit", requiredPermission = false)
    public ResponseData edit(@RequestBody @Validated(DictRequest.edit.class) DictRequest dictRequest) {
        this.dictService.edit(dictRequest);
        return new SuccessResponseData();
    }

    /**
     * 获取字典详情
     *
     * @author holly
     * @date 2020/10/29 16:35
     */
    @ApiOperation(value = "获取字典详情", notes = "获取字典详情")
    @GetResource(name = "获取字典详情", path = "/dict/detail", requiredPermission = false)
    public ResponseData detail(@Validated(BaseRequest.detail.class) DictRequest dictRequest) {
        SysDict detail = this.dictService.detail(dictRequest);
        return new SuccessResponseData(detail);
    }

    /**
     * 获取字典列表
     *
     * @author holly
     * @date 2020/10/29 16:35
     */
    @ApiOperation(value = "获取字典列表", notes = "获取字典列表")
    @GetResource(name = "获取字典列表", path = "/dict/list", requiredPermission = false)
    public ResponseData list(DictRequest dictRequest) {
        return new SuccessResponseData(this.dictService.findList(dictRequest));
    }

    /**
     * 获取字典列表(分页)
     *
     * @author holly
     * @date 2020/10/29 16:35
     */
    @ApiOperation(value = "获取字典列表", notes = "获取字典列表")
    @GetResource(name = "获取字典列表", path = "/dict/page", requiredPermission = false)
    public ResponseData page(DictRequest dictRequest) {
        return new SuccessResponseData(this.dictService.findPage(dictRequest));
    }


    /**
     * 获取树形字典列表（antdv在用）
     *
     * @author holly
     * @date 2020/10/29 16:36
     */
    @ApiOperation(value = "获取树形字典列表", notes = "获取树形字典列表")
    @GetResource(name = "获取树形字典列表", path = "/dict/getDictTreeList", requiredPermission = false)
    public ResponseData getDictTreeList(@Validated(DictRequest.treeList.class) DictRequest dictRequest) {
        List<TreeDictInfo> treeDictList = this.dictService.getTreeDictList(dictRequest);
        return new SuccessResponseData(treeDictList);
    }

    /**
     * 获取系统配置分组字典列表(分页)（给系统配置界面，左侧获取配置的分类用）
     *
     * @author holly
     * @date 2021/1/25 11:47
     */
    @ApiOperation(value = "获取系统配置分组字典列表", notes = "获取系统配置分组字典列表")
    @GetResource(name = "获取系统配置分组字典列表", path = "/dict/getConfigGroupPage", requiredPermission = false)
    public ResponseData getConfigGroupPage(DictRequest dictRequest) {
        dictRequest.setDictTypeCode(DictConstants.CONFIG_GROUP_DICT_TYPE_CODE);
        PageResult<SysDict> page = this.dictService.findPage(dictRequest);
        return new SuccessResponseData(page);
    }

    /**
     * 获取多语言字典列表(分页)（给多语言界面，左侧获取多语言的分类用）
     *
     * @author holly
     * @date 2021/1/25 11:47
     */
    @ApiOperation(value = "获取多语言字典列表", notes = "获取多语言字典列表")
    @GetResource(name = "获取多语言字典列表", path = "/dict/getLanguagesPage", requiredPermission = false)
    public ResponseData getLanguagesPage(DictRequest dictRequest) {
        dictRequest.setDictTypeCode(DictConstants.LANGUAGES_DICT_TYPE_CODE);
        PageResult<SysDict> page = this.dictService.findPage(dictRequest);
        return new SuccessResponseData(page);
    }

    /**
     * 获取字典列表
     *
     * @author holly
     * @date 2020/10/29 16:35
     */
    @ApiOperation(value = "根据类型获取字典列表", notes = "根据类型获取字典列表")
    @GetResource(name = "根据类型获取字典列表", path = "/dict/getDictByTypeCode", requiredPermission = false)
    public ResponseData getDictByTypeCode(String typeCode) {
        DictRequest dictRequest = new DictRequest();
        dictRequest.setDictTypeCode(typeCode);
        return new SuccessResponseData(this.dictService.findList(dictRequest));
    }


    @ApiOperation(value = "获取全部字典", notes = "获取全部字典")
    @GetResource(name = "获取全部字典列表", path = "/dict/getDictAll", requiredPermission = false)
    public ResponseData getDictAll() {
        List<Map<String, List<DictSelect>>> mapList = new ArrayList<>();
        LambdaQueryWrapper<SysDictType> typeQueryWrapper = new LambdaQueryWrapper<>();
        typeQueryWrapper.eq(SysDictType::getDelFlag, YesOrNotEnum.N.getCode());
        List<SysDictType> typeList = dictTypeService.list(typeQueryWrapper);
        LambdaQueryWrapper<SysDict> dictQueryWrapper = new LambdaQueryWrapper<>();
        dictQueryWrapper.eq(SysDict::getDelFlag, YesOrNotEnum.N.getCode());
        dictQueryWrapper.orderByAsc(SysDict::getDictSort);
        List<SysDict> dictList = this.dictService.list(dictQueryWrapper);
        if (ObjectUtil.isNotNull(typeList)) {
            mapList = typeList.stream().map(m -> {
                Map<String, List<DictSelect>> map = new HashMap<>();
                if (ObjectUtil.isNotNull(dictList)) {
                    List<DictSelect> list = dictList.stream().filter(n -> n.getDictTypeCode().equals(m.getDictTypeCode())).map(s -> {
                        DictSelect dictSelect = new DictSelect();
                        dictSelect.setText(s.getDictName());
                        dictSelect.setTitle(s.getDictName());
                        dictSelect.setValue(s.getDictCode());
                        return dictSelect;
                    }).collect(Collectors.toList());
                    map.put(m.getDictTypeCode(), list);
                } else {
                    map.put(m.getDictTypeCode(), new ArrayList<>());
                }
                return map;
            }).collect(Collectors.toList());
        }
        return new SuccessResponseData(mapList);
    }


}
